package cn.hznc.exception;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang.StringUtils;

import java.util.Collection;

/**
 * 断言异常工具类型
 *
 * @Author Administrator
 * @Date 2023/3/22 9:41
 */
public class AssertExUtil {

    /**
     * 判断对象是否为null
     *
     * @param object
     * @param toolManageEx
     * @param <T>
     * @throws ToolManageException
     */
    public static <T> void assertNoNull (T object,ToolManageEx toolManageEx) throws ToolManageException{
        if (ObjectUtil.isNull(object)){
            throw new ToolManageException(toolManageEx.getCode(),toolManageEx.getMsg());
        }
    }

    /**
     * 判断字符串是否为空
     *
     * @param str object
     * @param toolManageEx toolManageEx
     * @throws ToolManageException
     */
    public static void assertNotEmpty (String str,ToolManageEx toolManageEx) throws ToolManageException{
        if (StringUtils.isEmpty(str)){
            throw new ToolManageException(toolManageEx.getCode(),toolManageEx.getMsg());
        }
    }

    /**
     * 判断集合是否为null
     *
     * @param list list
     * @param toolManageEx toolManageEx
     * @param <T> 泛型
     * @throws ToolManageException
     */
    public static <T> void assertNotEmptyCol (Collection<T> list, ToolManageEx toolManageEx) throws ToolManageException{
        if (CollectionUtil.isEmpty(list)){
            throw new ToolManageException(toolManageEx.getCode(),toolManageEx.getMsg());
        }
    }
}
